package com.hadoop.mybatis.service.impl;

import com.hadoop.mybatis.mapper.UserMapper;
import com.hadoop.mybatis.po.User;
import com.hadoop.mybatis.service.UserService;
import com.hadoop.mybatis.vo.UserVo;
import com.hadoop.util.CommonUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p></p>
 *
 * @author Andy
 * @date 2017/10/11
 */
@Service(value = "userService")
@Transactional(value = "transactionManager")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int save(UserVo userVo) throws Exception {
        if (null != userVo.getBirthday()) {
            userVo.setAge(CommonUtil.getAge(userVo.getBirthday()));
        }

        if (StringUtils.isNotBlank(userVo.getPassword())) {
            userVo.setPassword(DigestUtils.md5Hex(userVo.getPassword()));
        }

        userVo.setCreateDate(new Date());
        userVo.setModifyDate(new Date());
        User user = new User();
        BeanUtils.copyProperties(userVo,user);
        return userMapper.insert(user);
    }

    @Override
    public User findUserByUserName(String userName) {
        return userMapper.findUserByUserName(userName);
    }
}
